package com.saleSystem.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.saleSystem.common.ServerResponse;
import com.saleSystem.dao.ContentMapper;
import com.saleSystem.pojo.Content;
import com.saleSystem.service.IProductService;

/**
 * 产品服务
 * @author guanwei
 *
 */
@Service("iProductService")
public class ProductServiceImpl implements IProductService {

	private static final Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);
	@Autowired
	private ContentMapper contentMapper;
	
	
	public ServerResponse<Boolean> deleteProduct(Integer id) {
		// TODO Auto-generated method stub
		try {
			int count = contentMapper.deleteByPrimaryKey(id);
			if(count > 0){
				return ServerResponse.createBySuccess("删除成功", true);
			}else{
				return ServerResponse.createByError("删除失败", false);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("数据库删除数据产生异常！",e);
			return ServerResponse.createByError("删除失败", false);
		}
	}

	public Content getContent(Integer id) {
		// TODO Auto-generated method stub
		if(id == null){
			return null;
		}
		Content content = contentMapper.selectByPrimaryKey(id);
		return content;
	}
	

	/**
	 * 新增内容信息
	 */
	public Content insertContent(Content content) {
		// TODO Auto-generated method stub
		
		if(content != null){
			try {
				int result = contentMapper.insertSelective(content);
				if(result > 0){
					Content newContent = contentMapper.selectContent(content.getTitle(), content.getImage(), content.getSummary());
					return newContent;
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				logger.error("数据库操作产生异常！",e);
			}
		}
		return null;
	}

	
	public List<Content> getAllContent(){
		
		List<Content> contentList = new ArrayList<Content>(); 
		try {
			contentList = contentMapper.selectAllContent();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("数据库查询产生异常！",e);
		}
		return contentList;
	}
	
	/**
	 * 编辑修改内容信息
	 */
	public Content editProduct(Integer id) {
		
		if(id != null){
			try {
				Content content = contentMapper.selectByPrimaryKey(id);
				return content;
			} catch (Exception e) {
				logger.error("数据库查询产生异常！",e);
			}
		}
		
		return null;
	}

	
	public Content updateContent(Content content) {
		// TODO Auto-generated method stub
		
		if(content != null){
			try {
				int result = contentMapper.updateByPrimaryKeySelective(content);
				if(result > 0){
					Content newContent = contentMapper.selectByPrimaryKey(content.getId());
					return newContent;
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				logger.error("数据库操作产生异常！",e);
			}
		}
		return null;
	}
	
	
}
